'use strict';

stareal
    .controller("AuthorPublishController", function ($scope, $stateParams, $api, $state, $alert, localStorageService,$interval,$window,$lazyLoader) {
        $scope.authorid=$stateParams.user_id;
        $scope.changeNav=$stateParams.kind;
        $scope.ationBtn=true;
        // console.log($scope.changeNav);
        var h=$(window).height()-5.7*parseFloat(document.documentElement.style.fontSize)+"px";
        $("#wrapper_container").height(h);
        if(localStorageService.get('token')!=''){
            $api.get("app/login/userinfo/retrieve",{},true)
                .then(function (ret) {
                    var myInfo=ret.data;
                    $scope.user = ret.data;
                    if(myInfo.id==$scope.authorid){
                        $scope.ationBtn=false;
                    }else{
                        $scope.ationBtn=true;
                    }

                })
        }
        $api.get("app/view/otherscenter",{authorid:$scope.authorid}, true).then(function (ret) {
            var  authorInfo=ret.data;
            $scope.authorInfo=authorInfo;

        })
        //用户关注
        $scope.viewAttention=function() {
            var  rs = "main.viewpersonal-" + JSON.stringify({user_id:$scope.authorid,kind:$scope.changeNav});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            if(isbind!=1&&openID!=''){
                localStorageService.set('cleartoken',token);
                location.href = "#/main/register/"+encodeURIComponent(rs);
                localStorageService.set('token','');
                return false
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.viewpersonal-" + JSON.stringify({user_id:$scope.authorid,kind:$scope.changeNav});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
            $api.post("app/follow/create",{author_id:$scope.authorid},true)
                .then(function (ret) {
                    if(ret.follow==1){
                        $scope.authorInfo.favor=1;
                        $alert.show("关注成功!");
                    }else{
                        $scope.authorInfo.favor=0;
                        $alert.show("取消关注!");
                    }
                },function (err){
                    $alert.show(err);
                })
        }
        //他人观演记列表
        $scope.getMoreclassifyli=function(){
            if($scope.changeNav==1){   //观演记
                $scope.pertoppic='static/img/img_bjm.png';
                $scope.headimgurlIcon='static/img/img_lujiao.png';
                $scope.goodviews = new $lazyLoader("app/view/talist",
                    {userid:$scope.authorid,flag:'1'}, true);
                $scope.goodviews.nextPage();
            }else if($scope.changeNav==2){ //评论
                //$scope.pertoppic='static/img/img_sdj_bjm.png';
                $scope.pertoppic='static/img/img_bjm.png';
                // $scope.headimgurlIcon='static/img/img_sdj_lujiao.png'
                $scope.headimgurlIcon='static/img/img_lujiao.png';
                $scope.goodcomments = new $lazyLoader("app/view/otherslist",
                    {id:$scope.authorid,flag:'1'}, true);
                $scope.goodcomments.nextPage();
            }
        }
        $scope.getMoreclassifyli();
        var refresh = function(){
            $state.go('main.viewpersonal',{user_id:$scope.authorid,kind:$scope.changeNav},true);
        };
        $scope.navSwitch=function(index){
            $scope.changeNav=index;
            refresh();
        }
        //点赞
        $scope.IsPraiseView = function (commentid,Index,num,from,event){  //tiao
            var  rs = "main.viewpersonal-" + JSON.stringify({user_id:$scope.authorid,kind:$scope.changeNav});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                if (isbind != 1 && openID != '') {
                    localStorageService.set('cleartoken', token);
                    location.href = "#/main/register/" + encodeURIComponent(rs);
                    localStorageService.set('token', '');
                    return false
                }
            }
            if (!localStorageService.get('token')){
                var  rs = "main.viewpersonal-" + JSON.stringify({user_id:$scope.authorid,kind:$scope.changeNav});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
            event.stopPropagation()//阻止冒泡
            $api.post("app/viewcomment/praise", {comment_id:commentid,type:''}, true)
                .then(function (ret) {
                    if(from=='detail'){//详情页
                        $scope.goodcomments.items[Index].is_praise = !  $scope.goodcomments.items[Index].is_praise;
                        $scope.goodcomments.items[Index].like = parseInt(ret.praise)+num;
                    }
                },function (err) {
                    $alert.show(err)
                })
        }
        //举报 操作
        $scope.showoperationdailog=function(ID,useID,detail){
            if (!localStorageService.get('token')) {
                var  rs = "main.viewpersonal-" + JSON.stringify({user_id:$scope.authorid,kind:$scope.changeNav});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
            if($scope.user.id==useID){
                $scope.deleteStatus=true;
            }else{
                $scope.deleteStatus=false;
            }
            $scope.selectReplyId=ID;
            $scope.selectReplydetail=detail;
            $scope.showoperationstatus=true;
        }
        $scope.hideoperationdailog=function(){
            $scope.showoperationstatus=false;
        }
        //回复 ui-sref="main.allreply({comment_id:review.id})"
        $scope.operationreply=function(event){
            event.stopPropagation()//阻止冒泡
            $state.go('main.viewallreply',{comment_id:$scope.selectReplyId},true);
        }
        //删除
        $scope.operationdelet=function(event){
            event.stopPropagation()//阻止冒泡
            $api.post("app/viewcomment/del", {
                comment_id: $scope.selectReplyId,
                type: ''
            }, true)
                .then(function (ret) {
                    $alert.show("删除成功!");
                    location.reload();

                }, function (err) {
                    $alert.show(err);

                })
        }
        //举报  app/viewcomment/report  type=1
        $scope.operationReport=function(content,event){
            event.stopPropagation()//阻止冒泡
            $api.post("app/viewcomment/report", {
                comment_id: $scope.selectReplyId,
                type: '',
                content:content
            }, true)
                .then(function (ret) {
                    $alert.show("举报成功!");
                    $scope.operationReportdailogstatus=false;
                }, function (err) {
                    $alert.show(err);

                })
        }
        //举报弹窗
        $scope.showReportdailog=function(event){
            event.stopPropagation()//阻止冒泡
            $scope.operationReportdailogstatus=true;
            $scope.showoperationstatus=false;
        }
        $scope.hideReportdailog=function(){
            $scope.operationReportdailogstatus=false;
        }
            //
    });